// @created: caoshi
// @date: 2021-07-17 21:35
package main

import (
	"errors"
	"fmt"
	"log"
	"net"
	"net/http"
	"net/rpc"
	"os"
)

// 算数运算结构体
type Arith struct {

}


// 算数运算请求结构体
type ArityRequest struct {
	A int
	B int
}

// 算数运算响应结构体
type ArityResponse struct {
	Pro int			// 乘积
	Quo int			// 商
	Rem int 		// 余数
}

// 乘法运算
func (ar *Arith) Multiply(req ArityRequest, res *ArityResponse) error {
	fmt.Println("========= rpc 客户端入参 :", req)
	res.Pro = req.A * req.B
	return nil
}

// 除法运算
func (ar *Arith) Divide(req ArityRequest, res *ArityResponse) error {
	fmt.Println("========= rpc 客户端入参 :", req)
	if req.B == 0 {
		return errors.New("divide by zero")
	}
	res.Quo = req.A / req.B
	res.Rem = req.A % req.B
	return nil
}

func main() {
	_ = rpc.Register(new(Arith))
	rpc.HandleHTTP()

	listen, err := net.Listen("tcp", "127.0.0.1:8095")

	if err != nil {
		log.Fatalln("fatal error: ", err)
	}

	fmt.Fprintf(os.Stdout, "%s", "start connection ")

	http.Serve(listen, nil)
}
